<?php

//<editor-fold defaultstate="collapsed" desc="License & Copyright">
/*
 * @license
 */

/**
 *
 * @author Jevgeni Geimanen (jevgeni.geimanen@gmail.com)
 */
//</editor-fold>

/**
 * @js-enum TYPE_ 
 */
abstract class SCFormField extends Component {

    public abstract function getFieldType();

    const TYPE_TEXT = 'TEXT';
    const TYPE_BUTTON = 'BUTTON';
    const TYPE_FILE = 'FILE';
    const TYPE_SELECT = 'SELECT';
    const TYPE_RADIO = 'RADIO';
    const TYPE_HIDDEN = 'HIDDEN';
    const TYPE_CHECKBOX = 'CHECKBOX';
    const TYPE_TEXTAREA = 'TEXTAREA';
    const TYPE_DATETIMEPICKER = 'DATETIMEPICKER';
    const TYPE_DATEPICKER = 'DATEPICKER';

    public function getLables() {
        return array();
    }
    
    /**
     *
     * @var FormFieldValidatorCollection 
     */
    private $_Validators = null;

    /**
     *
     * @return FormFieldValidatorCollection 
     */
    public function getValidators() {
        if ($this->_Validators === null) {
            $this->_Validators = new FormFieldValidatorCollection();
            $this->_Validators->setFormField($this);
        }
        return $this->_Validators;
    }

    /**
     *
     * @return RequiredFormFieldValidator
     */
    public function AddRequiredValidator() {
        return $this->getValidators()->AddRequiredValidator();
    }

    public function getMainTagName() {
        return "input";
    }

    protected function getIsMultiValue() {
        return false;
    }
    protected function getHasContents() {
        return false;
    }
    
    protected function CollectMainTagAttributes(&$attributes) {
        if ($this->getRenderOptionValue()!==null && in_array($this->getRenderOptionValue(),$this->getValue())) $attributes[$this->GetSelectedOptionAttributeName()] = 1;
        return parent::CollectMainTagAttributes($attributes);
    }

    //<editor-fold defaultstate="collapsed" desc="Options">
    /**
     * @var array
     */
    private $_Options = array();

    /**
     *
     * @return array
     */
    public function getOptions() {
        return $this->_Options;
    }

    /**
     *
     * @param array $options
     * @return FormField
     */
    public function setOptions($options) {
        $this->_Options = $options;
        return $this;
    }

//</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Label">
    /**
     * @var string
     */
    private $_Label = null;

    /**
     *
     * @return string
     */
    public function getLabel() {
        return $this->_Label;
    }

    /**
     *
     * @param string $label
     * @return Label 
     */
    public function setLabel($label) {
        $this->_Label = $label;
        return $this;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Value">
    /**
     * @var string
     */
    private $_Value = null;

    /**
     *
     * @return string
     */
    public function getValue() {
        $value = ($this->_Value == null ? $this->getDefaultValue() : $this->_Value);
        return $this->getIsMultiValue() ? (is_array($value) ? $value : array($value)) : $value;
    }

    /**
     *
     * @param string $value
     * @return SCFormField 
     */
    public function setValue($value) {
        $this->_Value = $value;
        return $this;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="DefaultValue">
    /**
     * @var string
     */
    private $_DefaultValue = null;

    /**
     *
     * @return string
     */
    public function getDefaultValue() {
        return $this->_DefaultValue;
    }

    /**
     *
     * @param string $DefaultValue
     * @return SCFormField 
     */
    public function setDefaultValue($DefaultValue) {
        $this->_DefaultValue = $DefaultValue;
        return $this;
    }

    //</editor-fold>

    public function Render() {
        switch ($this->getFieldType()) {
            
        }
        return parent::Render();
    }
    //<editor-fold defaultstate="collapsed" desc="RenderOptionValue">
    /**
     * @var object
     */
    private $_RenderOptionValue = null;

    /**
     *
     * @return object
     */
    public function getRenderOptionValue() {
        return $this->_RenderOptionValue;
    }

    /**
     *
     * @param object $option
     * @return RenderOptionValue 
     */
    public function setRenderOptionValue($option) {
        $this->_RenderOptionValue = $option;
        return $this;
    }

//</editor-fold>
    
    public function RenderOption($key) {
        $this->setRenderOptionValue($key);
        return $this->Render();
    }
    protected function GetSelectedOptionAttributeName() {
        return "checked";
    }

    public function CollectJSProperties(&$collection) {
        if (count($this->getValidators()) > 0) {
            $vals = array();
            $i = 0;
            foreach ($this->getValidators() as $val) {
                $vals["val{$i}"] = $val->getJSProperties();
                $i++;
            }
            $collection['validators'] = $vals;
        }
        $collection['type'] = $this->getFieldType();
        return parent::CollectJSProperties($collection);
    }

}

?>
